
package editor.map
{
	import editor.Notification.M2VNotification;
	import editor.Notification.V2MNotification;
	import editor.common.AppFacade;
	import editor.common.Common;
	import editor.common.UIManager;
	import editor.map.elements.AvatarNpc;
	import editor.map.elements.Npc;
	import editor.map.elements.Teleport;
	import editor.map.minimap.Minimap;
	
	import flash.display.Sprite;
	import flash.events.Event;
	import flash.geom.Point;

	
	/**
	 * @author 缺硒
	 */
	public class NpcLayer extends Sprite
	{
		private var _npcList:Vector.<AvatarNpc>;
		private var _gateList:Vector.<Teleport>;
		
		private var _inGate:Teleport;
		private var _outGate:Teleport;
		
		private var _preGate:Teleport;
		private var _nextGate:Teleport;
		
		// 构造函数
		public function NpcLayer()
		{
			_npcList = new Vector.<AvatarNpc>();
			addEventListener(Event.ENTER_FRAME, onEnterFrame);
		}

		private function onEnterFrame(evt:Event):void
		{
			AppFacade.getIntance().sendNotification(M2VNotification.UPDATE_GATES_INFO,
				{
					inX:_inGate != null ? _inGate.x : 0,
					inY:_inGate != null ? _inGate.y : 0,
					outX:_outGate != null ? _outGate.x : 0,
					outY:_outGate != null ? _outGate.y : 0,
					preX:_preGate != null ? _preGate.x : 0,
					preY:_preGate != null ? _preGate.y : 0,
					nextX:_nextGate != null ? _nextGate.x : 0,
					nextY:_nextGate != null ? _nextGate.y : 0
				});
		}
		
		public function get outGate():Teleport
		{
			return _outGate;
		}
		
		public function get inGate():Teleport
		{
			return _inGate;
		}
		
		public function get npcList():Vector.<AvatarNpc>
		{
			return _npcList;
		}
		
		
		/**
		 * 增加npc
		 * @param id 
		 */		
		public function addNpc(id:String):void
		{
			var npc:AvatarNpc = new AvatarNpc("1",mouseX, mouseY);
			addChild(npc);
			_npcList.push(npc);
			Common.ui.toolWindow.updateNpcList();
//			var npc:Npc = new Npc();
//			npc.x = mouseX;
//			npc.y = mouseY;
//			npc.setLogic();
//			npc.npcId = id;
//			addChild(npc);
//			_npcList.push(npc);
//			Common.ui.toolWindow.updateNpcList();
		}
			
		public function deleteNpc(npc:Npc):void
		{
			var id:int = _npcList.indexOf(npc);
			if(id > -1)
			{
				_npcList.splice(id, 1);
				if(this.contains(npc))
					this.removeChild(npc);
			}
			Common.ui.toolWindow.updateNpcList();
		}
		
		public function getNpcByIndex(idx:int):Npc
		{
			return _npcList[idx] as Npc;
		}
		
		
		/**
		 * 设置出城点 
		 */		
		public function addOutGate():void
		{
			if(_outGate == null)
			{
				_outGate = new Teleport(0x00ff00);
				_outGate.x = mouseX;
				_outGate.y = mouseY;
				_outGate.setLogic();
				_outGate.label = "出城点";
				_outGate.npcId = "出城点";
				addChild(_outGate);
				AppFacade.getIntance().sendNotification(M2VNotification.ADD_OUT_OK);
			}
			//
		}
		
		/**
		 * 设置进城点 
		 */		
		public function addInGate():void
		{
			if(_inGate == null)
			{
				_inGate = new Teleport(Minimap.MINI_IN_COLOR);
				_inGate.x = mouseX;
				_inGate.y = mouseY;
				_inGate.setLogic();
				_inGate.label = "进城点";
				_inGate.npcId = "进城点";
				addChild(_inGate);
				AppFacade.getIntance().sendNotification(M2VNotification.ADD_IN_OK);
			}
		}
		
		/**
		 * 增加链接门 
		 */		
		public function addLinkGate():void
		{
			var gate:Teleport = new Teleport(Minimap.MINI_PRE_COLOR);
			gate.x = mouseX;
			gate.y = mouseY;
			gate.setLogic();
			gate.label = "链接门";
			gate.npcId = "链接门";
			_gateList.push(gate);
			addChild(gate);
		}
		
		/**
		 * 删除链接门
		 * @param gate 
		 */		
		public function deleteLinkGate(gate:Teleport):void
		{
			var id:int = _gateList.indexOf(gate);
			if(id > -1)
			{
				_gateList.splice(id, 1);
				if(this.contains(gate))
					this.removeChild(gate);
			}
		}
		
		/**
		 * 设置前传送点
		 */		
		public function addPreGate():void
		{
			if(_preGate == null)
			{
				_preGate = new Teleport(Minimap.MINI_PRE_COLOR);
				_preGate.x = mouseX;
				_preGate.y = mouseY;
				_preGate.setLogic();
				_preGate.label = "前传送点";
				_preGate.npcId = "前传送点";
				addChild(_preGate);
				AppFacade.getIntance().sendNotification(M2VNotification.ADD_PRE_OK);
			}
		}
		
		/**
		 * 设置后传送点
		 */		
		public function addNextGate():void
		{
			if(_nextGate == null)
			{
				_nextGate = new Teleport(Minimap.MINI_NEXT_COLOR);
				_nextGate.x = mouseX;
				_nextGate.y = mouseY;
				_nextGate.setLogic();
				_nextGate.label = "下一个传送点";
				_nextGate.npcId = "下一个传送点";
				addChild(_nextGate);
				AppFacade.getIntance().sendNotification(M2VNotification.ADD_NEXT_OK);
			}
		}
		
		/**
		 * 清除layer 
		 */		
		public function clearLayer():void
		{
			Common.ui.toolWindow.clearSelectedNpc();
			_npcList = new Vector.<AvatarNpc>();
			_inGate = null;
			_preGate = null;
			_nextGate = null;
			while(numChildren > 0)
			{
				removeChildAt(0);
			}
			Common.ui.toolWindow.updateNpcList();
		}
		
		/**
		 * 重新初始化 
		 */		
		public function initLayer(npcList:Vector.<AvatarNpc>, inGate:Teleport, preGate:Teleport, nextGate:Teleport):void
		{
			clearLayer();
			_npcList = npcList;
			
			for each(var npc:Npc in _npcList)
			{
				addChild(npc);
			}
			
			_inGate 	= inGate;
			_preGate 	= preGate;
			_nextGate 	= nextGate;
			
			if(_inGate != null)
			addChild(_inGate);
			if(_preGate != null)
			addChild(_preGate);
			if(_nextGate != null)
			addChild(_nextGate);
			
//			Common.ui.gatesInfoWindow.initGateUI();
			Common.ui.toolWindow.updateNpcList();
		}

		public function get preGate():Teleport
		{
			return _preGate;
		}

		public function get nextGate():Teleport
		{
			return _nextGate;
		}

		public function get gateList():Vector.<Teleport>
		{
			return _gateList;
		}


		//
	}
}